package com.hyk.commonLib.common.utils.netRequest;


import androidx.annotation.CallSuper;

import com.hyk.commonLib.common.activity.BaseActivity;
import com.hyk.commonLib.common.entity.INetObj;
import com.hyk.commonLib.common.exception.CustomException;
import com.hyk.commonLib.common.utils.DialogUtils;
import com.hyk.commonLib.common.utils.ToastUtil;
import com.hyk.commonLib.common.utils.rx.EmptyObserver;
import com.lxj.xpopup.impl.LoadingPopupView;

import org.jetbrains.annotations.NotNull;

import java.lang.ref.WeakReference;

public abstract class CustomizeNetObserver<OBJ extends INetObj<DATA>, DATA> extends EmptyObserver<OBJ> {

    private final WeakReference<LoadingPopupView> dialogWeakReference;
    private final WeakReference<BaseActivity<?>> activityWeakReference;

    private boolean showErrorDialog = true;
    private boolean showErrorMsg = true;

    public CustomizeNetObserver(BaseActivity<?> baseActivity, boolean showProgress) {
        this(baseActivity, showProgress ? DialogUtils.showProgressDialog(baseActivity, DialogUtils.PROGRESS_TITLE_WAITING) : null);
    }

    public CustomizeNetObserver(BaseActivity<?> baseActivity, LoadingPopupView progressDialog) {
        activityWeakReference = new WeakReference<>(baseActivity);
        dialogWeakReference = new WeakReference<>(progressDialog);
    }

    public CustomizeNetObserver(BaseActivity<?> baseActivity) {
        this(baseActivity, true);
    }

    public CustomizeNetObserver() {
        this(null, false);
        setShowErrorDialog(false);
    }

    @Override
    public void onNext(@NotNull OBJ netObj) {
        progressDismiss();
        onGetObj(netObj);
        if (INetObj.isSuccess(netObj)) {
            onGetSuccessObj(netObj.getData());
        } else {
            if (netObj.isTokenExpired()) {
                if (onTokenExpired()) return;
            }
            if (showErrorDialog) {
                DialogUtils.showServerRequestFailDialog(activityWeakReference.get(), netObj);
            }
            onGetErrorObj(netObj);
            onError(netObj.getMessage());
        }
        onFinish();
    }

    @Override
    @CallSuper
    public void onError(@NotNull Throwable e) {
        progressDismiss();
        String errMsg;
        if (e instanceof CustomException) {
            errMsg = e.getMessage();
        } else {
            errMsg = "网络连接异常，请稍后重试~";
        }
        if (showErrorMsg) {
            ToastUtil.getSingleton().showError(errMsg);
        }
        onError(errMsg);
        onFinish();
    }

    @Override
    @CallSuper
    public void onComplete() {
        progressDismiss();
    }

    private void progressDismiss() {
        if (dialogWeakReference == null) return;
        LoadingPopupView progressDialog = dialogWeakReference.get();
        if (progressDialog == null) return;
        if (activityWeakReference == null) return;
        BaseActivity<?> baseActivity = activityWeakReference.get();
        if (baseActivity == null) return;
        DialogUtils.closeProgressDialog(baseActivity, progressDialog);
    }

    protected void onGetObj(OBJ netObj) {

    }

    protected abstract void onGetSuccessObj(DATA data);

    protected void onGetErrorObj(OBJ netObj) {
        if (showErrorMsg) {
            ToastUtil.getSingleton().showError(netObj.getMessage());
        }
    }

    /**
     * 失败回调
     */
    protected void onError(String errMsg) {

    }

    /**
     * 无论成功失败都会调用
     */
    protected void onFinish() {

    }

    /**
     * @return 子类是否已处理。返回false代表继续回调 onGetErrorObj(t) onError() onFinish()；返回true不回调
     */
    protected boolean onTokenExpired() {
        return false;
    }

    public CustomizeNetObserver<OBJ, DATA> setShowErrorDialog(boolean showErrorDialog) {
        this.showErrorDialog = showErrorDialog;
        return this;
    }

    public CustomizeNetObserver<OBJ, DATA> setShowErrorMsg(boolean showErrorMsg) {
        this.showErrorMsg = showErrorMsg;
        return this;
    }
}
